/** Keriosity, ESIR
 * 	AutoActivity (Observer): activitee permettant d'envoyer Keriosity automatiquement sur une destination GPS
 *	@author: GOUYET Brett
 *	@date: 11/03/2012, derniere modif 20/03
 */

package keriosity.activities;

import keriosity.core.Auto;
import keriosity.core.IAuto;

import org.osmdroid.views.MapView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.activity.keriosity.R;


public class AutoActivity extends Activity implements IAutoObserver {
	
	protected IAuto _auto = null; //notre Observable
	protected String _urlOrder = ""; //url ou l'on envoi les ordres de mission sur le serveur
	protected String _urlGPS = ""; //url ou l'on va recuperer la position GPS du robot
	
	//attributs de connection
	private EditText _nameRobot = null; 
	private Button _connexion = null;	
	private Button _deconnexion = null;
	private Button _manual = null; //on passe a l'activite manuel
	
	//attributs de mission
	private Button _send = null;
	private CheckBox _picture = null; //photo
	private EditText _mail = null; //adresse mail
	private Button _undoMap = null;
	private Button _cleanMap = null;
	private Button _findKer = null; //permet de retrouver Keriosity sur la carte
	private Button _menu = null; //retour au menu
	
	//notre carte
	protected MapView _mapView;
		
	   
    public void onCreate(Bundle savedInstanceState) {
    	
    	super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_automatic);
		
		_urlOrder = "/mission?"; 
		_urlGPS	= "/getPosition";
		
		//on s'occupe de la connection:
		_nameRobot = (EditText) findViewById(R.id.automatic_nameConnexion);
		_connexion = (Button) findViewById(R.id.automatic_connexion);   
		_deconnexion = (Button) findViewById(R.id.automatic_deconnexion); 
		_manual = (Button) findViewById(R.id.automatic_manual);
   
	    //attributs de mission
		_send = (Button) findViewById(R.id.automatic_send);
		_mail = (EditText)findViewById(R.id.automatic_nameMail);
		_picture = (CheckBox)findViewById(R.id.picture);
		_undoMap = (Button) findViewById(R.id.automatic_undo);
		_cleanMap = (Button) findViewById(R.id.automatic_clean);
		_findKer = (Button) findViewById(R.id.automatic_findKer);	   
		_menu = (Button) findViewById(R.id.automatic_menu);
	   
	    //carte
		_mapView = (MapView) this.findViewById(R.id.automatic_mapview); //on charge la carte    	
	   
		_auto = new Auto(this); //on crée notre Observable
	   
	  
	    	/** LISTENERS **/
	
	   //Listener de connexion
		_connexion.setOnClickListener(new OnClickListener() {
	       	
	    	public void onClick(View v) {
	    		
	    		if(_auto.connectionWithWifi(_nameRobot.getText().toString()))	    		
    				Toast.makeText(getApplicationContext(),getString(R.string.connection_wifi), Toast.LENGTH_SHORT).show();  
    			else
    				Toast.makeText(getApplicationContext(),getString(R.string.error_wifi), Toast.LENGTH_SHORT).show();
	    	}        			
		});  
        
		_deconnexion.setOnClickListener(new OnClickListener() {
		
			public void onClick(View v) {
		
				_auto.disconnect(); //on se deconnecte
				Toast.makeText(getApplicationContext(),getString(R.string.deconnection), Toast.LENGTH_SHORT).show();  
			}
		});
       
		_manual.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
		
				_auto.disconnect(); //on se deconnecte
				
				Intent intent = new Intent(AutoActivity.this, ManualActivity.class); //on change d'activite
				startActivity(intent);
		}});
        
        
        //Listener de mission
		_send.setOnClickListener(new OnClickListener() {
           	
	    	public void onClick(View v) {    		
	    		
	    		//on envoie l'ordre avec les donnees de missions
	    		int mess = _auto.send(_picture.isChecked(), _mail.getText().toString()); 
	    		
	    		//on gere les erreurs
	    		if(mess == -1)
	    			Toast.makeText(getApplicationContext(),getString(R.string.error_connexion_1), Toast.LENGTH_SHORT).show();    
	    		
	    		if(mess == 1)		
		    		Toast.makeText(getApplicationContext(),getString(R.string.error_connexion_2), Toast.LENGTH_SHORT).show();    
		    	 
	    		if(mess == 2)	
	    			Toast.makeText(getApplicationContext(), getString(R.string.error_connexion_3), Toast.LENGTH_SHORT).show();    
	    	}
       }); 
               
		_menu.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
		
				_auto.disconnect(); //on se deconnecte
				
				Intent intent = new Intent(AutoActivity.this, HomeActivity.class); //on change d'activite
				startActivity(intent);
		}});
                 	
       
        //Listener clean undo et findKer
        _cleanMap.setOnClickListener(new OnClickListener() {
           	
	    	public void onClick(View v) {  
	    		_auto.clean();
	    	}
       });  
       
        _undoMap.setOnClickListener(new OnClickListener() {
           	
	    	public void onClick(View v) {  
	    		_auto.undo(); 
	    	}
       });  

        _findKer.setOnClickListener(new OnClickListener() {
           	
	    	public void onClick(View v) {  
	    		_auto.find(); 
	    	}
       });  
        
    }
    
    public MapView getMapView(){
    	return _mapView;
    }

	public String getUrlOrder() {
		return _urlOrder;
	}

	public String getUrlGPS() {
		return _urlGPS;
	}
}  